package com.gdzl_hadoop.util;

import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFRow;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;

public class DbfUtil {

//    private static final String YXTCHD = "YXTCHD";//有效土层厚度(0,30],(30-60],(60-100],(100,)
//    private static final String BCTRZD = "BCTRZD";//表层土壤质地
//    private static final String PMGX = "PMGX";//剖面构型
//    private static final String TRYJZHL = "TRYJZHL";//土壤有机质含量(0,6],(6,10],(10,20],(20,30]，（30，】
//    private static final String TRSJD = "TRSJD";//土壤酸碱度<0,4.5],(4.5,  5],(5.0,5.5],(5.5,6.0],(6.0-7.9],(7.9,8.5],(8.5,9], (9,]
//    private static final String PSTJ = "PSTJ";//排水条件
//    private static final String DXPD = "DXPD";//地形坡度
//    private static final String GGBZL = "GGBZL";//灌溉保证率
//    private static final String TMPD = "TMPD";//田面坡度
//    private static final String DXSW = "DXSW";//地下水位(0,30]、(30,60]、(60,)

    /**
     * 使用10个指标，去掉3个数据多为1的指标，使用默认分段预处理
     *
     * @param file
     */

    public static StringBuffer readXJFDDY(File file) {
        DBFReader reader = null;
        DBFRow row = null;
        StringBuffer sb = new StringBuffer();
        try {
            reader = new DBFReader(new FileInputStream(file), Charset.forName("GBK"));
            while ((row = reader.nextRow()) != null) {
                int temp;
                double temp2;
                temp = row.getInt("YXTCHD");
                if (temp <= 30) {
                    temp = 30;
                    sb.append("YXTCHD:" + temp + ",");
                } else if (temp > 30 && temp <= 60) {
                    temp = 60;
                    sb.append("YXTCHD:" + temp + ",");
                } else if (temp > 60 && temp <= 100) {
                    temp = 60;
                    sb.append("YXTCHD:" + temp + ",");
                } else {
                    String YXTCHD = "more";
                    sb.append("YXTCHD:" + YXTCHD + ",");
                }

                String BCTRZD = row.getString("BCTRZD");
                sb.append("BCTRZD:" + BCTRZD + ",");

                String PMGX = row.getString("PMGX");
                sb.append("PMGX:" + PMGX + ",");

                temp2 = row.getDouble("TRYJZHL");
                if (temp2 <= 6) {
                    temp2 = 6;
                    sb.append("TRYJZHL:" + temp2 + ",");
                } else if (temp2 > 6 && temp2 <= 10) {
                    temp2 = 10;
                    sb.append("TRYJZHL:" + temp2 + ",");
                } else if (temp2 > 10 && temp2 <= 20) {
                    temp2 = 20;
                    sb.append("TRYJZHL:" + temp2 + ",");
                } else if (temp2 > 20 && temp2 <= 30) {
                    temp2 = 30;
                    sb.append("TRYJZHL:" + temp2 + ",");
                } else {
                    sb.append("TRYJZHL:more,");
                }

                temp2 = row.getDouble("TRSJD");
                if (temp2 <= 4.5) {
                    temp2 = 4.5;
                    sb.append("TRSJD:" + temp2 + ",");
                } else if (temp2 > 4.5 && temp2 <= 5) {
                    temp2 = 5;
                    sb.append("TRSJD:" + temp2 + ",");
                } else if (temp2 > 5 && temp2 <= 5.5) {
                    temp2 = 5.5;
                    sb.append("TRSJD:" + temp2 + ",");
                } else if (temp2 > 5.5 && temp2 <= 6) {
                    temp2 = 6;
                    sb.append("TRSJD:" + temp2 + ",");
                } else if (temp2 > 6 && temp2 <= 7.9) {
                    temp2 = 7.9;
                    sb.append("TRSJD:" + temp2 + ",");
                } else if (temp2 > 7.9 && temp2 <= 8.5) {
                    temp2 = 8.5;
                    sb.append("TRSJD:" + temp2 + ",");
                } else if (temp2 > 8.5) {
                    temp2 = 9;
                    sb.append("TRSJD:" + temp2 + ",");
                }

                int PSTJ = row.getInt("PSTJ");
                sb.append("PSTJ:" + PSTJ + ",");

                String DXPD = row.getString("DXPD");
                sb.append("DXPD:" + DXPD + ",");

                int GGBZL = row.getInt("GGBZL");
                sb.append("GGBZL:" + GGBZL + ",");

                String TMPD = row.getString("TMPD");
                sb.append("TMPD:" + TMPD + ",");

                temp = row.getInt("DXSW");
                if (temp <= 30) {
                    temp = 30;
                    sb.append("DXSW:" + temp + "\n");
                } else if (temp > 30 && temp <= 60) {
                    temp = 60;
                    sb.append("DXSW:" + temp + "\n");
                } else if (temp > 60) {
                    sb.append("DXSW:more\n");
                }
            }//endwhile
            sb.deleteCharAt(sb.length() - 1);
            return sb;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int countTrans(File dbfFile) {
        try {
            DBFReader reader = new DBFReader(new FileInputStream(dbfFile), Charset.forName("GBK"));
            System.out.println(reader.getRecordCount());
            return reader.getRecordCount();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
